package com.battery.system.service.battery.impl;

import java.util.List;

import com.battery.common.enums.EventStateEnum;
import com.battery.common.enums.YesOrNoEnum;
import com.battery.common.utils.DateUtils;
import com.battery.system.domain.BO.EventBO;
import com.battery.system.domain.VO.EventVO;
import com.battery.system.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.battery.system.mapper.EventMapper;
import com.battery.system.domain.Event;
import com.battery.system.service.battery.IEventService;
import com.battery.common.core.text.Convert;
import tk.mybatis.mapper.entity.Example;

/**
 * 活动Service业务层处理
 *
 * @author battery
 * @date 2021-01-26
 */
@Service
public class EventServiceImpl extends BaseServiceImpl<Event> implements IEventService {


    @Autowired
    private EventMapper eventMapper;

    /**
     * 查询活动
     *
     * @param id 活动ID
     * @return 活动
     */
    @Override
    public Event selectEventById(String id) {
        return eventMapper.selectEventById(id);
    }

    /**
     * 查询活动列表
     *
     * @param event 活动
     * @return 活动
     */
    @Override
    public List<Event> selectEventList(Event event) {
        return eventMapper.selectEventList(event);
    }

    /**
     * 新增活动
     *
     * @param event 活动
     * @return 结果
     */
    @Override
    public int insertEvent(Event event) {
        return eventMapper.insertEvent(event);
    }

    /**
     * 修改活动
     *
     * @param event 活动
     * @return 结果
     */
    @Override
    public int updateEvent(Event event) {
        return eventMapper.updateEvent(event);
    }

    /**
     * 删除活动对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteEventByIds(String ids) {
        return eventMapper.deleteEventByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除活动信息
     *
     * @param id 活动ID
     * @return 结果
     */
    @Override
    public int deleteEventById(String id) {
        return eventMapper.deleteEventById(id);
    }

    /**
     * 活动列表
     *
     * @param eventBO
     * @return
     */
    @Override
    public List<EventVO> selectEventVOList(EventBO eventBO) {
        return eventMapper.selectEventVOList(eventBO);
    }


    @Override
    public Event selectByEventName(String eventName) {
        Example example = new Example(Event.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("eventName", eventName);
        criteria.andEqualTo("isDelete", YesOrNoEnum.NO.getCode());
        return eventMapper.selectOneByExample(example);
    }


    @Override
    public int deleteLogicEventById(String id) {
        Event eventUpdate = new Event();
        eventUpdate.setId(id);
        eventUpdate.setIsDelete(YesOrNoEnum.YES.getCode());
        return eventMapper.updateByPrimaryKeySelective(eventUpdate);
    }

    /**
     * 活动开启或者关闭
     *
     * @param id
     * @param open
     * @return
     */
    @Override
    public int openOrClose(String id, Integer open) {
        Event eventUpdate = new Event();
        eventUpdate.setId(id);
        if (open.intValue() == EventStateEnum.OPEN.getCode().intValue()) {
            eventUpdate.setState(EventStateEnum.OPEN.getCode());
        } else {
            eventUpdate.setState(EventStateEnum.CLOSE.getCode());
        }
        eventUpdate.setUpdatedTime(DateUtils.getNowDate());
        return eventMapper.updateByPrimaryKeySelective(eventUpdate);
    }


    /**
     * 查询可选择的活动
     *
     * @return
     */
    @Override
    public List<EventVO> averageEventList() {
        return eventMapper.averageEventList();
    }


}
